package com.bbzn.device.client.service.impl;

import com.bbzn.device.client.dto.index.PlanLeaderDTO;
import com.bbzn.device.client.utils.BabaBeanUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.bbzn.device.client.dataobject.PlanLeader;
import com.bbzn.device.client.dao.planleader.PlanLeaderMapper;
import com.bbzn.device.client.service.PlanLeaderService;

@Service
public class PlanLeaderServiceImpl implements PlanLeaderService {

    @Resource
    private PlanLeaderMapper planLeaderMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return planLeaderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteByLeaderId(Long id) {
        return planLeaderMapper.deleteByLeaderId(id);
    }

    @Override
    public int insert(PlanLeader record) {
        return planLeaderMapper.insert(record);
    }

    @Override
    public int insertOrUpdate(PlanLeader record) {
        return planLeaderMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(PlanLeader record) {
        return planLeaderMapper.insertOrUpdateSelective(record);
    }

    @Override
    public int insertSelective(PlanLeader record) {
        return planLeaderMapper.insertSelective(record);
    }

    @Override
    public PlanLeader selectByPrimaryKey(Long id) {
        return planLeaderMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(PlanLeader record) {
        return planLeaderMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(PlanLeader record) {
        return planLeaderMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<PlanLeader> findByAll(PlanLeader planLeader) {
        return planLeaderMapper.findByAll(planLeader);
    }

    @Override
    public int updateBatch(List<PlanLeader> list) {
        return planLeaderMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<PlanLeader> list) {
        return planLeaderMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<PlanLeader> list) {
        return planLeaderMapper.batchInsert(list);
    }

    @Override
    public List<PlanLeaderDTO> getPlanLeaderList(Long id) {
        List<PlanLeaderDTO> leaderList= planLeaderMapper.getPlanLeaderList(id);
        //JDK8的stream处理,把根分类区分出来
        List<PlanLeaderDTO> roots = leaderList.stream().filter(lead -> (lead.getPid() == 0)).
                map(p -> {
                    PlanLeaderDTO node = BabaBeanUtils.voToDto(p, PlanLeaderDTO.class);
                    return node;
                }).collect(Collectors.toList());
        //对根分类进行排序
        roots.sort(new Comparator<PlanLeaderDTO>() {
            @Override
            public int compare(PlanLeaderDTO o1, PlanLeaderDTO o2) {
                return o1.getId() > o2.getId() ? 1 : -1;
            }
        });

        //把非根分类区分出来
        List<PlanLeaderDTO> subs = leaderList.stream().filter(p -> (p.getPid() != 0)).map(p -> {
            PlanLeaderDTO subNode = BabaBeanUtils.voToDto(p, PlanLeaderDTO.class);
            return subNode;
        }).collect(Collectors.toList());

        //递归构建结构化的分类信息
        roots.forEach(root -> buildSubs(root, subs));
        return roots;
    }

    /**
     * 递归构建
     *
     * @param parent
     * @param subs
     */
    private void buildSubs(PlanLeaderDTO parent, List<PlanLeaderDTO> subs) {
        List<PlanLeaderDTO> children = subs.stream().filter(sub -> (sub.getPid().equals(parent.getId()))).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(children)) {//有子分类的情况
            parent.setChildren(children);
            children.forEach(child -> buildSubs(child, subs));//再次递归构建
        }
    }

}


